//============================================================================
// Copyright (C) 2008-2013 Brett R. Jones 
//
// Issued to public domain 2013
//
// brettjones1900@gmail.com
// http://www.p2panarchy.com
//============================================================================
#include "VxSktUdpBroadcast.h"

//============================================================================
//=== constructor ===//
VxSktUdpBroadcast::VxSktUdpBroadcast()
{
	m_eSktType = eSktTypeUdpBroadcast;
}

//============================================================================
//=== destructor ===//
VxSktUdpBroadcast::~VxSktUdpBroadcast()
{
}

//============================================================================
//! startup broadcast socket if pBroadcastIp is null
//! then will broadcast to 255.255.255.255 
RCODE VxSktUdpBroadcast::UdpBroadcastOpen(	VxInetAddress& oIpAddr,
											U16 u16Port,	
											const char * pBroadcastIp )
{
	m_oLclIp.setPort( u16Port );
	m_oRmtIp.setPort( u16Port );
	if( pBroadcastIp )
	{
		m_strBroadcastIp = pBroadcastIp;
		m_strRmtIp = pBroadcastIp;
		m_oBroadcastIp.setIp( pBroadcastIp );
	}
	else
	{
		m_strBroadcastIp = "255.255.255.255";
		m_strRmtIp = m_strBroadcastIp;
		m_oBroadcastIp.setIp( m_strBroadcastIp.c_str() );
	}
	m_oRmtIp.setIp( m_strBroadcastIp.c_str() );

	// BRJ dont broadcast
	/*
	// open socket
	RCODE rc = VxSktUdp::UdpOpen( oIpAddr, u16Port );
	if( rc )
	{
		return rc;
	}
	// tell socket to broadcast
	BOOL bBroadcast = true;
	if( SOCKET_ERROR == setsockopt( m_oSocket, SOL_SOCKET,SO_BROADCAST, (const char *)&bBroadcast, sizeof(bBroadcast)) ) 
	{
		m_rcLastError = VxGetLastError();
		log_msg( 0, "VxSktUdpBroadcast:setsockopt error %s\n", DescribeSktError( m_rcLastError ) );
		return m_rcLastError;
	}
	return 0;
	*/
	return -1;
}

//============================================================================
//! broadcast data 
RCODE VxSktUdpBroadcast::BroadcastData(	const char *	pData,		// data to send
										int				iDataLen,	// data length
										U16				u16Port )	// port to send to ( if 0 then port specified when opened )
{
	return SendTo( pData, iDataLen, m_oBroadcastIp, u16Port );
}



